<!DOCTYPE html>
<html lang="en">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1">
	<title>使用领域专用语言(DSL)进行查询 | ElasticSearch 7.7 权威指南中文版</title>
	<meta name="keywords" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <meta name="description" content="ElasticSearch 权威指南中文版, elasticsearch 7, es7, 实时数据分析，实时数据检索" />
    <!-- Give IE8 a fighting chance -->
    <!--[if lt IE 9]>
    <script src="https://oss.maxcdn.com/html5shiv/3.7.2/html5shiv.min.js"></script>
    <script src="https://oss.maxcdn.com/respond/1.4.2/respond.min.js"></script>
    <![endif]-->
	<link rel="stylesheet" type="text/css" href="../static/styles.css" />
	<script>
	var _link = 'query-dsl.html';
    </script>
</head>
<body>
<div class="main-container">
    <section id="content">
        <div class="content-wrapper">
            <section id="guide" lang="zh_cn">
                <div class="container">
                    <div class="row">
                        <div class="col-xs-12 col-sm-8 col-md-8 guide-section">
                            <div style="color:gray; word-break: break-all; font-size:12px;">原英文版地址: <a href="https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl.html" rel="nofollow" target="_blank">https://www.elastic.co/guide/en/elasticsearch/reference/7.7/query-dsl.html</a>, 原文档版权归 www.elastic.co 所有<br/>本地英文版地址: <a href="../en/query-dsl.html" rel="nofollow" target="_blank">../en/query-dsl.html</a></div>
                        <!-- start body -->
                  <div class="page_header">
<strong>重要</strong>: 此版本不会发布额外的bug修复或文档更新。最新信息请参考 <a href="https://www.elastic.co/guide/en/elasticsearch/reference/current/index.html" rel="nofollow">当前版本文档</a>。
</div>
<div id="content">
<div class="breadcrumbs">
<span class="breadcrumb-link"><a href="index.html">Elasticsearch 权威指南 [7.7]</a></span>
»
<span class="breadcrumb-node">查询 DSL</span>
</div>
<div class="navheader">
<span class="prev">
<a href="modules-cross-cluster-search.html">« 跨集群搜索</a>
</span>
<span class="next">
<a href="query-filter-context.html">query 和 filter 上下文 »</a>
</span>
</div>
<div class="part">
<div class="titlepage"><div><div>
<h1 class="title">
<a id="query-dsl"></a>Query DSL: 使用领域专用语言进行查询
</h1>
</div></div></div>
<div class="openblock partintro">
<div class="content">
<p>
Elasticsearch 提供了一个完整的基于 JSON 的 Query DSL（Domain Specific Language, 领域专用语言）来定义查询。

可以把 Query DSL 想象成查询的 AST(Abstract Syntax Tree, 抽象语法树)，由两种类型的子句组成：
</p>
<div class="variablelist">
<dl class="variablelist">
<dt>
<span class="term">
叶子查询子句 (leaf query clauses)
</span>
</dt>
<dd>
叶子查询子句查找特定字段中的特定值，例如 <a class="xref" href="query-dsl-match-query.html" title="Match query"><code class="literal">match</code></a>、<a class="xref" href="query-dsl-term-query.html" title="Term query"><code class="literal">term</code></a> 或 <a class="xref" href="query-dsl-range-query.html" title="Range query"><code class="literal">range</code></a> 查询。

这些查询可以单独使用。
</dd>
<dt>
<span class="term">
复合查询子句 (compound query clauses)
</span>
</dt>
<dd>
复合查询子句包裹其他叶子查询 <span class="strong strong"><strong>或</strong></span> 复合查询，用于以逻辑方式组合多个查询(如 <a class="xref" href="query-dsl-bool-query.html" title="Boolean query"><code class="literal">bool</code></a> 或 <a class="xref" href="query-dsl-dis-max-query.html" title="Disjunction max query"><code class="literal">dis_max</code></a>查询)，或改变它们的行为(如 <a class="xref" href="query-dsl-constant-score-query.html" title="Constant score query"><code class="literal">constant_score</code></a> 查询)。
</dd>
</dl>
</div>
<p>
根据查询子句是用于 <a class="xref" href="query-filter-context.html" title="Query and filter context">query 还是 filter 的上下文</a> 中，它们的行为会有所不同。
</p>
<div class="variablelist">
<a id="query-dsl-allow-expensive-queries"></a>
<dl class="variablelist">
<dt>
<span class="term">
允许执行昂贵的查询
</span>
</dt>
<dd>
<p>
某些类型的查询通常会因其实现方式而执行缓慢，这会影响集群的稳定性。

这些查询可以分类如下：
</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
<p>需要进行线性扫描来识别匹配的查询：</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
<a class="xref" href="query-dsl-script-query.html" title="Script query"><code class="literal">script 查询</code></a>
</li>
</ul>
</div>
</li>
<li class="listitem">
<p>具有较高的前期成本的查询：</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
<a class="xref" href="query-dsl-fuzzy-query.html" title="Fuzzy query"><code class="literal">fuzzy 查询</code></a>
</li>
<li class="listitem">
<a class="xref" href="query-dsl-regexp-query.html" title="Regexp query"><code class="literal">regexp 查询</code></a>
</li>
<li class="listitem">
不带 <a class="xref" href="index-prefixes.html" title="index_prefixes"><code class="literal">index_prefixes</code></a> 的 <a class="xref" href="query-dsl-prefix-query.html" title="Prefix query"><code class="literal">prefix 查询</code></a> 
</li>
<li class="listitem">
<a class="xref" href="query-dsl-wildcard-query.html" title="Wildcard query"><code class="literal">wildcard 查询</code></a>
</li>
<li class="listitem">
在 <a class="xref" href="text.html.html" title="Text datatype"><code class="literal">text</code></a> 和 <a class="xref" href="keyword.html" title="Keyword datatype"><code class="literal">keyword</code></a> 字段上的 <a class="xref" href="query-dsl-range-query.html" title="Range query">range 查询</a> 
</li>
</ul>
</div>
</li>
<li class="listitem">
<a class="xref" href="joining-queries.html" title="Joining queries"><code class="literal">联结查询 (joining queries)</code></a>
</li>
<li class="listitem">
对 <a class="xref" href="geo-shape.html#prefix-trees" title="Prefix trees">已弃用的地理形状</a> 的查询
</li>
<li class="listitem">
<p>每个文档的成本可能很高的查询：</p>
<div class="ulist itemizedlist">
<ul class="itemizedlist">
<li class="listitem">
<a class="xref" href="query-dsl-script-score-query.html" title="Script score query"><code class="literal">script score 查询</code></a>
</li>
<li class="listitem">
<a class="xref" href="query-dsl-percolate-query.html" title="Percolate query"><code class="literal">percolate 查询</code></a>
</li>
</ul>
</div>
</li>
</ul>
</div>
</dd>
</dl>
</div>
<p>
通过将 <code class="literal">search.allow_expensive_queries</code>设置为<code class="literal">false</code> (默认为 <code class="literal">true</code>)，可以防止执行此类查询。
</p>
</div>
</div>













</div>
<div class="navfooter">
<span class="prev">
<a href="modules-cross-cluster-search.html">« 跨集群搜索</a>
</span>
<span class="next">
<a href="query-filter-context.html">query 和 filter 上下文 »</a>
</span>
</div>
</div>

                  <!-- end body -->
                        </div>
                        <div class="col-xs-12 col-sm-4 col-md-4" id="right_col">
                        
                        </div>
                    </div>
                </div>
            </section>
        </div>
    </section>
</div>
<script src="../static/cn.js"></script>
</body>
</html>